<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
  <style>
    .wrap {
      width: 100%;
      display: flex;
      margin: 0.2rem 0 0 0;
      position: relative;
    }
    
    /*左侧的导航样式*/
    .nav_left {
      width: 21.1875rem;
      overflow: scroll;
    }
    
    .nav_left::-webkit-scrollbar {
      display: none;
    }
    
    .nav_content {
      white-space: nowrap;
      padding: 0 0.7rem;
    }
    
    .nav_content span {
      display: inline-block;
      padding: 0.4rem 0.6rem;
      font-size: 0.875rem;
    }
    
    .nav_content .active {
      border-bottom: 2px solid #7f4395;
      color: #7f4395;
    }
    
    .nav_left,
    .down {
      float: left;
    }
    
    /*右侧导航部分*/
    .down {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .gray {
      color: gray;
      display: inline-block;
      vertical-align: middle;
    }
  </style>
</head>
<body>
  <div id="app">
    <div>
      <span>姓名:</span>
      <input type="text" v-model.trim="username" />
    </div>
    <div>
      <span>年龄:</span>
      <input type="number" v-model.number="userAge" />
    </div>
    <div>
      <span>性别:</span>
      <select v-model="gender">
        <option value="男">男</option>
        <option value="女">女</option>
      </select>
    </div>
    <div>
      <button @click="addOrEdit">添加/修改</button>
    </div>
    <div>
      <table
        border="1"
        cellpadding="10"
        cellspacing="0"
        v-show="dataArr.length > 0"
      >
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
          <th>操作</th>
        </tr>
        <tr v-for="(item, index) in dataArr" :key="item.name">
          <td>{{ index + 1 }}</td>
          <td>{{ item.name }}</td>
          <td>{{ item.age }}</td>
          <td>{{ item.sex }}</td>
          <td>
            <button @click="del(index)">删除</button>
            <button @click="edit(index)">编辑</button>
          </td>
        </tr>
      </table>
    </div>
  </div>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        username: "",
        userAge: 0,
        gender: "男",
        editIndex: null, // 保存正在编辑的对象的索引, 千万不能给0和0以上的值(默认上来就是编辑状态)
        dataArr: [
          {
            name: "Tom",
            age: 19,
            sex: "男",
          },
          {
            name: "Jone",
            age: 21,
            sex: "女",
          },
        ],
      },
      methods: {
        addOrEdit() {
          // 添加/修改-点击事件方法
          const newObj = {
            name: this.username,
            age: this.userAge,
            sex: this.gender,
          }
          if (this.editIndex !== null) { // 本次是要编辑更新
            this.dataArr.splice(this.editIndex, 1, newObj)
            // 千万注意
            this.editIndex = null // 更新后, 保证下次点击是新增
          } else { // 新增
            this.dataArr.push(newObj)
          }
        },
        del(index) {
          // 删除 点击事件方法
          this.dataArr.splice(index, 1)
        },
        edit(index) {
          // 编辑 点击事件方法 -index(正在编辑的数据对象的索引值)
          let editObj = this.dataArr[index] // 把要编辑的对象信息取出来
          this.username = editObj.name
          this.userAge = editObj.age
          this.gender = editObj.sex
          this.editIndex = index // 把正在编辑的索引值保存起来
        }
      }
    })
  </script>
</body>
</html>